<?php
class Lib_ImageUtil {

    public static function getImageMime($img, $type='file'){
        $mime = FALSE ;
        if($type=='blob'){
            $img = base64_decode($img) ;
            if(function_exists('getimagesizefromstring')){
                $size = getimagesizefromstring($img) ;
                $mime = $size['mime'] ;
            }else{
                $file = sprintf('/tmp/%s', mt_rand(1000,9999).time()) ;
                file_put_contents($file, $img) ;
                $img = $file ;
            }
        }elseif($type=='file' || isset($file)){
            if(function_exists('getimagesize')){
                $size = getimagesize($img) ;
                $mime = $size['mime'] ;
            }else{
                $shell = 'file -i -b %s' ;
                ob_start() ;
                system(sprintf($shell, $img)) ;
                $mime = ob_get_clean() ; 
            }
        }
        return $mime ;
    }

    public static function getExtFromMime($mime){
        static $_mimes = array(
            'image/jpeg'=>'jpg',
            'image/pjpeg'=>'jpg',
            'image/gif'=>'gif',
            'image/png'=>'png',
            'image/x-png'=>'png',
            'application/octet-stream'=>'jpg',
        ) ;
        return isset($_mimes[$mime]) ? $_mimes[$mime] : 'jpg' ;
    }
}
